<script>
    jQuery().ready(function(){
        // 营销事项申请表单
        var fieldIdXb = WfForm.convertFieldNameToId("sfhtbz");

        addParams();

        changeDetails();

        WfForm.registerAction(WfForm.ACTION_ADDROW+"1", function(index){
            changeDetails();
        });
        WfForm.registerAction(WfForm.ACTION_DELROW+"1", function(arg){
            changeDetails();
        });
        WfForm.registerAction(WfForm.ACTION_EDITDETAILROW+"1", function(arg){
            changeDetails();
        });
        WfForm.registerAction(WfForm.ACTION_SWITCHDETAILPAGING, function(groupid){
            changeDetails();
        });
        WfForm.registerAction(WfForm.ACTION_SWITCHTABLAYOUT, function(tabid){
            changeDetails();
        });

        WfForm.bindFieldChangeEvent(fieldIdXb, function (obj, id, value) {
            window.setTimeout(function () {
                if (value == 0) {
                    jQuery(".yz_class_1").removeClass("detail_hide_col");
                    jQuery(".yz_class_2").removeClass("detail_hide_col");
                    jQuery(".yz_class_3").removeClass("detail_hide_col");
                    jQuery(".yz_class_4").removeClass("detail_hide_col");
                    jQuery(".yz_class_5").removeClass("detail_hide_col");
                    jQuery(".yz_class_6").removeClass("detail_hide_col");
                    jQuery(".yz_class_7").removeClass("detail_hide_col");
                    jQuery(".yz_class_8").removeClass("detail_hide_col");
                } else if (value == 1) {
                    jQuery(".yz_class_1").addClass("detail_hide_col");
                    jQuery(".yz_class_2").addClass("detail_hide_col");
                    jQuery(".yz_class_3").addClass("detail_hide_col");
                    jQuery(".yz_class_4").addClass("detail_hide_col");
                    jQuery(".yz_class_5").addClass("detail_hide_col");
                    jQuery(".yz_class_6").addClass("detail_hide_col");
                    jQuery(".yz_class_7").addClass("detail_hide_col");
                    jQuery(".yz_class_8").addClass("detail_hide_col");
                }
                if (value == null || value == "") {
                    jQuery(".yz_class_1").removeClass("detail_hide_col");
                    jQuery(".yz_class_2").removeClass("detail_hide_col");
                    jQuery(".yz_class_3").removeClass("detail_hide_col");
                    jQuery(".yz_class_4").removeClass("detail_hide_col");
                    jQuery(".yz_class_5").removeClass("detail_hide_col");
                    jQuery(".yz_class_6").removeClass("detail_hide_col");
                    jQuery(".yz_class_7").removeClass("detail_hide_col");
                    jQuery(".yz_class_8").removeClass("detail_hide_col");
                }
            }, 10);
        });

        // 监听合同编号 htbh 修改 sfgxht 0
        var htbhXb = WfForm.convertFieldNameToId("htbh");
        var sfgxhtXb = WfForm.convertFieldNameToId("sfgxht");

        var fieldIdSfhtbz = WfForm.convertFieldNameToId("sfhtbz");
        var fieldValueSfhtbz = WfForm.getFieldValue(fieldIdSfhtbz);

        WfForm.bindFieldChangeEvent(htbhXb, function (obj, id, value) {
            window.setTimeout(function () {
                var index = value.indexOf(',');
                if(index !== -1){
                    WfForm.changeFieldValue(sfgxhtXb, {value:"0"});
                }else{
                    WfForm.changeFieldValue(sfgxhtXb, {value:""});
                }
                if(fieldValueSfhtbz == 0){
                    console.log("value:"+value);
                    var htjjsxhjName = WfForm.convertFieldNameToId("htjjsxhj");
                    getJJSXId(value,htjjsxhjName);
                }else{
                    console.log('不是合同报账！');
                }
            }, 10);
        });

    });

    function getQueryString(name){
    //解析浏览器链接参数
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var r = window.location.href.substr(1).match(reg);
        if (r != null) {
            return decodeURI(r[2]);
        }
        return null;
    }

    function changeDetails(){
        var fieldIdXb = WfForm.convertFieldNameToId("sfhtbz");
        var fieldvaluez = WfForm.getFieldValue(fieldIdXb);

        if (fieldvaluez == 1) {
            jQuery(".yz_class_1").addClass("detail_hide_col");
            jQuery(".yz_class_2").addClass("detail_hide_col");
            jQuery(".yz_class_3").addClass("detail_hide_col");
            jQuery(".yz_class_4").addClass("detail_hide_col");
            jQuery(".yz_class_5").addClass("detail_hide_col");
            jQuery(".yz_class_6").addClass("detail_hide_col");
            jQuery(".yz_class_7").addClass("detail_hide_col");
            jQuery(".yz_class_8").addClass("detail_hide_col");
        }else {
            jQuery(".yz_class_1").removeClass("detail_hide_col");
            jQuery(".yz_class_2").removeClass("detail_hide_col");
            jQuery(".yz_class_3").removeClass("detail_hide_col");
            jQuery(".yz_class_4").removeClass("detail_hide_col");
            jQuery(".yz_class_5").removeClass("detail_hide_col");
            jQuery(".yz_class_6").removeClass("detail_hide_col");
            jQuery(".yz_class_7").removeClass("detail_hide_col");
            jQuery(".yz_class_8").removeClass("detail_hide_col");
        }
    }

    function addParams(){
        // 营销平台活动授权码
        var actAuthCode = getQueryString("ACT_AUTH_CODE");
        if(actAuthCode != null && actAuthCode != ""){
            // 流水号
            var code = getQueryString("SERIAL_CODE");
            // 申请人
            var applicant = getQueryString("APPLICANT");
            // 项目编号
            var productCode = getQueryString("PROJECT_CODE");
            // 系統來源
            var source = getQueryString("SOURCE");
            // 表单号
            var codeName = WfForm.convertFieldNameToId("yxptlsh");
            // 申请人
            var applicantName = WfForm.convertFieldNameToId("sqr");
            // 项目编码
            var productCodeName = WfForm.convertFieldNameToId("PROJECT_CODE");
            // 营销平台备注
            var bzName = WfForm.convertFieldNameToId("yxptbz");
            // 來源系統
            var lyxtName = WfForm.convertFieldNameToId("lyxt");

            WfForm.changeFieldValue(codeName, {value:code});

            console.log(applicant+"——"+applicantName);
            getUserId(applicant,applicantName);

            WfForm.changeFieldValue(productCodeName, {value:productCode});
            WfForm.changeFieldValue(bzName, {value:actAuthCode});
            WfForm.changeFieldValue(lyxtName, {value:source});
        }else{
            console.log("不是三方链接！");
        }
    }

    function getUserId(code,fieldidSqr){
        jQuery.ajax({
            url: "/api/market/update/changeUserId",
            dataType: "json",
            async: false,
            contentType: 'application/json',
            type: "POST",
            data: JSON.stringify({code: code}),
            success: function (data) {
                if (data !== '') {
                    if (data.errorCode === 1) {
                        var uId = data.data
                        console.log(uId);
                        window.setTimeout(function () {
                            WfForm.changeFieldValue(fieldidSqr, uId);
                        }, 50);
                    } else {
                        console.log("error："+data.msg);
                    }
                }
            },
            error: function (data) {
                if (data !== '') {
                    console.log("error："+data.msg);
                }
            }
        });
    }

    function getJJSXId(code,fieldidSqr){
        jQuery.ajax({
            url: "/api/market/update/changeJJSXId",
            dataType: "json",
            async: false,
            contentType: 'application/json',
            type: "POST",
            data: JSON.stringify({code: code}),
            success: function (data) {
                if (data !== '') {
                    if (data.errorCode === 1) {
                        var uId = data.data
                        console.log(uId);
                        window.setTimeout(function () {
                            WfForm.changeFieldValue(fieldidSqr, {value:uId});
                        }, 50);
                    } else {
                        console.log("error："+data.msg);
                    }
                }
            },
            error: function (data) {
                if (data !== '') {
                    console.log("error："+data.msg);
                }
            }
        });
    }
</script>